{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "440a667b-d5e6-4cfe-b34c-0773605eeabf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "存入 500 元，当前余额：1500 元\n",
      "取出 200 元，当前余额：1300 元\n",
      "1300\n"
     ]
    }
   ],
   "source": [
    "class BankAccount:\n",
    "    def __init__(self, owner, balance=0):\n",
    "        self.owner = owner\n",
    "        self.__balance = balance  # 私有属性（双下划线开头）\n",
    "\n",
    "    def deposit(self, amount):\n",
    "        if amount > 0:\n",
    "            self.__balance += amount\n",
    "            print(f\"存入 {amount} 元，当前余额：{self.__balance} 元\")\n",
    "        else:\n",
    "            print(\"存款金额必须大于0\")\n",
    "\n",
    "    def withdraw(self, amount):\n",
    "        if 0 < amount <= self.__balance:\n",
    "            self.__balance -= amount\n",
    "            print(f\"取出 {amount} 元，当前余额：{self.__balance} 元\")\n",
    "        else:\n",
    "            print(\"余额不足或金额无效\")\n",
    "\n",
    "    def get_balance(self):  # 提供“安全”的查看余额方式\n",
    "        return self.__balance\n",
    "\n",
    "# 使用\n",
    "account = BankAccount(\"张三\", 1000)\n",
    "account.deposit(500)\n",
    "account.withdraw(200)\n",
    "print(account.get_balance())  # 1300\n",
    "\n",
    "# ❌ 不能直接访问私有属性（会报错）\n",
    "# print(account.__balance)  # AttributeError!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ebc1cf29-2ad3-4da2-8bbf-412ff14b720c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n"
     ]
    }
   ],
   "source": [
    "print(account.owner)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3a8e8140-1225-4ac8-b404-5c82531e59ef",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'BankAccount' object has no attribute '__balance'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mAttributeError\u001b[39m                            Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43maccount\u001b[49m\u001b[43m.\u001b[49m\u001b[43m__balance\u001b[49m)\n",
      "\u001b[31mAttributeError\u001b[39m: 'BankAccount' object has no attribute '__balance'"
     ]
    }
   ],
   "source": [
    "print(account.__balance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3d652449-ed45-42bb-9a30-8b377a525216",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "旺财 汪汪叫！\n",
      "咪咪 喵喵叫！\n",
      "咪咪 爬上了树！\n",
      "卓拉跟 发出了声音...\n"
     ]
    }
   ],
   "source": [
    "class Animal:  # 父类\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "\n",
    "    def speak(self):\n",
    "        print(f\"{self.name} 发出了声音...\")\n",
    "\n",
    "class Dog(Animal):  # 子类，继承 Animal\n",
    "    def speak(self):  # 重写父类方法\n",
    "        print(f\"{self.name} 汪汪叫！\")\n",
    "\n",
    "class Cat(Animal):  # 子类\n",
    "    def speak(self):\n",
    "        print(f\"{self.name} 喵喵叫！\")\n",
    "\n",
    "    def climb_tree(self):  # 子类特有方法\n",
    "        print(f\"{self.name} 爬上了树！\")\n",
    "\n",
    "class Dragon(Animal):\n",
    "    def fire(self):\n",
    "        print(f\"{self.name} 喷火啦！\")\n",
    "# 使用\n",
    "d = Dog(\"旺财\")\n",
    "c = Cat(\"咪咪\")\n",
    "e = Dragon(\"卓拉跟\")\n",
    "d.speak()  # 旺财 汪汪叫！\n",
    "c.speak()  # 咪咪 喵喵叫！\n",
    "c.climb_tree()  # 咪咪 爬上了树！\n",
    "e.speak()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9983fbb0-3837-450b-80c1-95b4bbb9b2ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vector(2, 3)\n",
      "Vector(3, 7)\n"
     ]
    }
   ],
   "source": [
    "class Vector:\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "\n",
    "    def __str__(self):  # 定义 print 时的显示\n",
    "        return f\"Vector({self.x}, {self.y})\"\n",
    "\n",
    "    def __add__(self, other):  # 定义 + 操作\n",
    "        return Vector(self.x + other.x, self.y + other.y)\n",
    "\n",
    "v1 = Vector(2, 3)\n",
    "v2 = Vector(1, 4)\n",
    "\n",
    "print(v1)           # Vector(2, 3)\n",
    "print(v1 + v2)      # Vector(3, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b09b072-4417-4957-8718-138ad885ebf6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
